home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / cofflink.c < prev    next >
C/C++ Source or Header  |  1994-10-10  |  59KB  |  2,164 lines

  1. /* COFF specific linker code.
  2.    Copyright 1994 Free Software Foundation, Inc.
  3.    Written by Ian Lance Taylor, Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This file contains the COFF backend linker code.  */
  22.  
  23. #include "bfd.h"
  24. #include "sysdep.h"
  25. #include "bfdlink.h"
  26. #include "libbfd.h"
  27. #include "coff/internal.h"
  28. #include "libcoff.h"
  29.  
  30. #define STRING_SIZE_SIZE (4)
  31.  
  32. /* Information we keep for each section in the output file when doing
  33.    a relocateable link.  */
  34.  
  35. struct coff_link_section_info
  36. {
  37.   /* The relocs to be output.  */
  38.   struct internal_reloc *relocs;
  39.   /* For each reloc against a global symbol whose index was not known
  40.      when the reloc was handled, the global hash table entry.  */
  41.   struct coff_link_hash_entry **rel_hashes;
  42. };
  43.  
  44. /* Information that we pass around while doing the final link step.  */
  45.  
  46. struct coff_final_link_info
  47. {
  48.   /* General link information.  */
  49.   struct bfd_link_info *info;
  50.   /* Output BFD.  */
  51.   bfd *output_bfd;
  52.   /* Used to indicate failure in traversal routine.  */
  53.   boolean failed;
  54.   /* Hash table for long symbol name.  */
  55.   struct bfd_strtab_hash *strtab;
  56.   /* When doing a relocateable link, an array of information kept for
  57.      each output section, indexed by the target_index field.  */
  58.   struct coff_link_section_info *section_info;
  59.   /* Symbol index of last C_FILE symbol (-1 if none).  */
  60.   long last_file_index;
  61.   /* Contents of last C_FILE symbol.  */
  62.   struct internal_syment last_file;
  63.   /* Buffer large enough to hold swapped symbols of any input file.  */
  64.   struct internal_syment *internal_syms;
  65.   /* Buffer large enough to hold sections of symbols of any input file.  */
  66.   asection **sec_ptrs;
  67.   /* Buffer large enough to hold output indices of symbols of any
  68.      input file.  */
  69.   long *sym_indices;
  70.   /* Buffer large enough to hold output symbols for any input file.  */
  71.   bfd_byte *outsyms;
  72.   /* Buffer large enough to hold external line numbers for any input
  73.      section.  */
  74.   bfd_byte *linenos;
  75.   /* Buffer large enough to hold any input section.  */
  76.   bfd_byte *contents;
  77.   /* Buffer large enough to hold external relocs of any input section.  */
  78.   bfd_byte *external_relocs;
  79.   /* Buffer large enough to hold swapped relocs of any input section.  */
  80.   struct internal_reloc *internal_relocs;
  81. };
  82.  
  83. static struct bfd_hash_entry *coff_link_hash_newfunc
  84.   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
  85. static boolean coff_link_add_object_symbols
  86.   PARAMS ((bfd *, struct bfd_link_info *));
  87. static boolean coff_link_check_archive_element
  88.   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
  89. static boolean coff_link_get_symbols PARAMS ((bfd *));
  90. static const char *coff_read_string_table PARAMS ((bfd *));
  91. static boolean coff_link_free_symbols PARAMS ((bfd *));
  92. static boolean coff_link_check_ar_symbols
  93.   PARAMS ((bfd *, struct bfd_link_info *, boolean *));
  94. static boolean coff_link_add_symbols PARAMS ((bfd *, struct bfd_link_info *));
  95. static boolean coff_link_input_bfd
  96.   PARAMS ((struct coff_final_link_info *, bfd *));
  97. static boolean coff_write_global_sym
  98.   PARAMS ((struct coff_link_hash_entry *, PTR));
  99. static boolean coff_reloc_link_order
  100.   PARAMS ((bfd *, struct coff_final_link_info *, asection *,
  101.        struct bfd_link_order *));
  102.  
  103. /* Create an entry in a COFF linker hash table.  */
  104.  
  105. static struct bfd_hash_entry *
  106. coff_link_hash_newfunc (entry, table, string)
  107.      struct bfd_hash_entry *entry;
  108.      struct bfd_hash_table *table;
  109.      const char *string;
  110. {
  111.   struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
  112.  
  113.   /* Allocate the structure if it has not already been allocated by a
  114.      subclass.  */
  115.   if (ret == (struct coff_link_hash_entry *) NULL)
  116.     ret = ((struct coff_link_hash_entry *)
  117.        bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
  118.   if (ret == (struct coff_link_hash_entry *) NULL)
  119.     {
  120.       bfd_set_error (bfd_error_no_memory);
  121.       return (struct bfd_hash_entry *) ret;
  122.     }
  123.  
  124.   /* Call the allocation method of the superclass.  */
  125.   ret = ((struct coff_link_hash_entry *)
  126.      _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  127.                  table, string));
  128.   if (ret != (struct coff_link_hash_entry *) NULL)
  129.     {
  130.       /* Set local fields.  */
  131.       ret->indx = -1;
  132.       ret->type = T_NULL;
  133.       ret->class = C_NULL;
  134.       ret->numaux = 0;
  135.       ret->auxbfd = NULL;
  136.       ret->aux = NULL;
  137.     }
  138.  
  139.   return (struct bfd_hash_entry *) ret;
  140. }
  141.  
  142. /* Create a COFF linker hash table.  */
  143.  
  144. struct bfd_link_hash_table *
  145. _bfd_coff_link_hash_table_create (abfd)
  146.      bfd *abfd;
  147. {
  148.   struct coff_link_hash_table *ret;
  149.  
  150.   ret = ((struct coff_link_hash_table *)
  151.      malloc (sizeof (struct coff_link_hash_table)));
  152.   if (ret == NULL)
  153.     {
  154.       bfd_set_error (bfd_error_no_memory);
  155.       return NULL;
  156.     }
  157.   if (! _bfd_link_hash_table_init (&ret->root, abfd,
  158.                    coff_link_hash_newfunc))
  159.     {
  160.       free (ret);
  161.       return (struct bfd_link_hash_table *) NULL;
  162.     }
  163.   return &ret->root;
  164. }
  165.  
  166. /* Given a COFF BFD, add symbols to the global hash table as
  167.    appropriate.  */
  168.  
  169. boolean
  170. _bfd_coff_link_add_symbols (abfd, info)
  171.      bfd *abfd;
  172.      struct bfd_link_info *info;
  173. {
  174.   switch (bfd_get_format (abfd))
  175.     {
  176.     case bfd_object:
  177.       return coff_link_add_object_symbols (abfd, info);
  178.     case bfd_archive:
  179.       return (_bfd_generic_link_add_archive_symbols
  180.           (abfd, info, coff_link_check_archive_element));
  181.     default:
  182.       bfd_set_error (bfd_error_wrong_format);
  183.       return false;
  184.     }
  185. }
  186.  
  187. /* Add symbols from a COFF object file.  */
  188.  
  189. static boolean
  190. coff_link_add_object_symbols (abfd, info)
  191.      bfd *abfd;
  192.      struct bfd_link_info *info;
  193. {
  194.   if (! coff_link_get_symbols (abfd))
  195.     return false;
  196.   if (! coff_link_add_symbols (abfd, info))
  197.     return false;
  198.   if (! info->keep_memory)
  199.     {
  200.       if (! coff_link_free_symbols (abfd))
  201.     return false;
  202.     }
  203.   return true;
  204. }
  205.  
  206. /* Check a single archive element to see if we need to include it in
  207.    the link.  *PNEEDED is set according to whether this element is
  208.    needed in the link or not.  This is called via
  209.    _bfd_generic_link_add_archive_symbols.  */
  210.  
  211. static boolean
  212. coff_link_check_archive_element (abfd, info, pneeded)
  213.      bfd *abfd;
  214.      struct bfd_link_info *info;
  215.      boolean *pneeded;
  216. {
  217.   if (! coff_link_get_symbols (abfd))
  218.     return false;
  219.  
  220.   if (! coff_link_check_ar_symbols (abfd, info, pneeded))
  221.     return false;
  222.  
  223.   if (*pneeded)
  224.     {
  225.       if (! coff_link_add_symbols (abfd, info))
  226.     return false;
  227.     }
  228.  
  229.   if (! info->keep_memory || ! *pneeded)
  230.     {
  231.       if (! coff_link_free_symbols (abfd))
  232.     return false;
  233.     }
  234.  
  235.   return true;
  236. }
  237.  
  238. /* Read in the external symbols.  */
  239.  
  240. static boolean
  241. coff_link_get_symbols (abfd)
  242.      bfd *abfd;
  243. {
  244.   bfd_size_type symesz;
  245.   size_t size;
  246.   PTR syms;
  247.  
  248.   if (obj_coff_external_syms (abfd) != NULL)
  249.     return true;
  250.  
  251.   symesz = bfd_coff_symesz (abfd);
  252.  
  253.   size = obj_raw_syment_count (abfd) * symesz;
  254.  
  255.   syms = malloc (size);
  256.   if (syms == NULL && size != 0)
  257.     {
  258.       bfd_set_error (bfd_error_no_memory);
  259.       return false;
  260.     }
  261.  
  262.   if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
  263.       || bfd_read (syms, size, 1, abfd) != size)
  264.     {
  265.       if (syms != NULL)
  266.     free (syms);
  267.       return false;
  268.     }
  269.  
  270.   obj_coff_external_syms (abfd) = syms;
  271.  
  272.   return true;
  273. }
  274.  
  275. /* Read in the external strings.  The strings are not loaded until
  276.    they are needed.  This is because we have no simple way of
  277.    detecting a missing string table in an archive.  */
  278.  
  279. static const char *
  280. coff_read_string_table (abfd)
  281.      bfd *abfd;
  282. {
  283.   char extstrsize[STRING_SIZE_SIZE];
  284.   size_t strsize;
  285.   char *strings;
  286.  
  287.   if (obj_coff_strings (abfd) != NULL)
  288.     return obj_coff_strings (abfd);
  289.  
  290.   if (bfd_seek (abfd,
  291.         (obj_sym_filepos (abfd)
  292.          + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
  293.         SEEK_SET) != 0)
  294.     return NULL;
  295.     
  296.   if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
  297.     {
  298.       if (bfd_get_error () != bfd_error_file_truncated)
  299.     return NULL;
  300.  
  301.       /* There is no string table.  */
  302.       strsize = STRING_SIZE_SIZE;
  303.     }
  304.   else
  305.     {
  306. #if STRING_SIZE_SIZE == 4
  307.       strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
  308. #else
  309.  #error Change bfd_h_get_32
  310. #endif
  311.     }
  312.  
  313.   strings = malloc (strsize);
  314.   if (strings == NULL)
  315.     {
  316.       bfd_set_error (bfd_error_no_memory);
  317.       return NULL;
  318.     }
  319.  
  320.   if (bfd_read (strings + STRING_SIZE_SIZE,
  321.         strsize - STRING_SIZE_SIZE, 1, abfd)
  322.       != strsize - STRING_SIZE_SIZE)
  323.     {
  324.       free (strings);
  325.       return NULL;
  326.     }
  327.  
  328.   obj_coff_strings (abfd) = strings;
  329.  
  330.   return strings;
  331. }
  332.  
  333. /* Free up the external symbols and strings read from a COFF file.  */
  334.  
  335. static boolean
  336. coff_link_free_symbols (abfd)
  337.      bfd *abfd;
  338. {
  339.   if (obj_coff_external_syms (abfd) != NULL)
  340.     {
  341.       free (obj_coff_external_syms (abfd));
  342.       obj_coff_external_syms (abfd) = NULL;
  343.     }
  344.   if (obj_coff_strings (abfd) != NULL)
  345.     {
  346.       free (obj_coff_strings (abfd));
  347.       obj_coff_strings (abfd) = NULL;
  348.     }
  349.   return true;
  350. }
  351.  
  352. /* Look through the symbols to see if this object file should be
  353.    included in the link.  */
  354.  
  355. static boolean
  356. coff_link_check_ar_symbols (abfd, info, pneeded)
  357.      bfd *abfd;
  358.      struct bfd_link_info *info;
  359.      boolean *pneeded;
  360. {
  361.   boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
  362.   const char *strings;
  363.   bfd_size_type symesz;
  364.   bfd_byte *esym;
  365.   bfd_byte *esym_end;
  366.  
  367.   *pneeded = false;
  368.  
  369.   sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
  370.   strings = NULL;
  371.  
  372.   symesz = bfd_coff_symesz (abfd);
  373.   esym = (bfd_byte *) obj_coff_external_syms (abfd);
  374.   esym_end = esym + obj_raw_syment_count (abfd) * symesz;
  375.   while (esym < esym_end)
  376.     {
  377.       struct internal_syment sym;
  378.  
  379.       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
  380.  
  381.       if ((sym.n_sclass == C_EXT
  382.        || (sym_is_global && (*sym_is_global) (abfd, &sym)))
  383.       && (sym.n_scnum != 0 || sym.n_value != 0))
  384.     {
  385.       const char *name;
  386.       char buf[SYMNMLEN + 1];
  387.       struct bfd_link_hash_entry *h;
  388.  
  389.       /* This symbol is externally visible, and is defined by this
  390.              object file.  */
  391.  
  392.       /* FIXME: It's not clear this will work correctly if sizeof
  393.              (_n_zeroes) != 4.  */
  394.       if (sym._n._n_n._n_zeroes != 0
  395.           || sym._n._n_n._n_offset == 0)
  396.         {
  397.           memcpy (buf, sym._n._n_name, SYMNMLEN);
  398.           buf[SYMNMLEN] = '\0';
  399.           name = buf;
  400.         }
  401.       else
  402.         {
  403.           BFD_ASSERT (sym._n._n_n._n_offset >= STRING_SIZE_SIZE);
  404.           if (strings == NULL)
  405.         {
  406.           strings = coff_read_string_table (abfd);
  407.           if (strings == NULL)
  408.             return false;
  409.         }
  410.           name = strings + sym._n._n_n._n_offset;
  411.         }
  412.  
  413.       h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  414.  
  415.       /* We are only interested in symbols that are currently
  416.          undefined.  If a symbol is currently known to be common,
  417.          COFF linkers do not bring in an object file which defines
  418.          it.  */
  419.       if (h != (struct bfd_link_hash_entry *) NULL
  420.           && h->type == bfd_link_hash_undefined)
  421.         {
  422.           if (! (*info->callbacks->add_archive_element) (info, abfd, name))
  423.         return false;
  424.           *pneeded = true;
  425.           return true;
  426.         }
  427.     }
  428.  
  429.       esym += (sym.n_numaux + 1) * symesz;
  430.     }
  431.  
  432.   /* We do not need this object file.  */
  433.   return true;
  434. }
  435.  
  436. /* Add all the symbols from an object file to the hash table.  */
  437.  
  438. static boolean
  439. coff_link_add_symbols (abfd, info)
  440.      bfd *abfd;
  441.      struct bfd_link_info *info;
  442. {
  443.   boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
  444.   const char *strings;
  445.   boolean default_copy;
  446.   bfd_size_type symcount;
  447.   struct coff_link_hash_entry **sym_hash;
  448.   bfd_size_type symesz;
  449.   bfd_byte *esym;
  450.   bfd_byte *esym_end;
  451.  
  452.   sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
  453.   strings = NULL;
  454.  
  455.   if (info->keep_memory)
  456.     default_copy = false;
  457.   else
  458.     default_copy = true;
  459.  
  460.   symcount = obj_raw_syment_count (abfd);
  461.  
  462.   /* We keep a list of the linker hash table entries that correspond
  463.      to particular symbols.  */
  464.   sym_hash = ((struct coff_link_hash_entry **)
  465.           bfd_alloc (abfd,
  466.              ((size_t) symcount
  467.               * sizeof (struct coff_link_hash_entry *))));
  468.   if (sym_hash == NULL && symcount != 0)
  469.     {
  470.       bfd_set_error (bfd_error_no_memory);
  471.       return false;
  472.     }
  473.   obj_coff_sym_hashes (abfd) = sym_hash;
  474.   memset (sym_hash, 0,
  475.       (size_t) symcount * sizeof (struct coff_link_hash_entry *));
  476.  
  477.   symesz = bfd_coff_symesz (abfd);
  478.   BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
  479.   esym = (bfd_byte *) obj_coff_external_syms (abfd);
  480.   esym_end = esym + symcount * symesz;
  481.   while (esym < esym_end)
  482.     {
  483.       struct internal_syment sym;
  484.       boolean copy;
  485.  
  486.       bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
  487.  
  488.       if (sym.n_sclass == C_EXT
  489.       || (sym_is_global && (*sym_is_global) (abfd, &sym)))
  490.     {
  491.       const char *name;
  492.       char buf[SYMNMLEN + 1];
  493.       flagword flags;
  494.       asection *section;
  495.       bfd_vma value;
  496.  
  497.       /* This symbol is externally visible.  */
  498.  
  499.       /* FIXME: It's not clear this will work correctly if sizeof
  500.              (_n_zeroes) != 4.  */
  501.       copy = default_copy;
  502.       if (sym._n._n_n._n_zeroes == 0
  503.           && sym._n._n_n._n_offset != 0)
  504.         {
  505.           BFD_ASSERT (sym._n._n_n._n_offset >= STRING_SIZE_SIZE);
  506.           if (strings == NULL)
  507.         {
  508.           strings = coff_read_string_table (abfd);
  509.           if (strings == NULL)
  510.             return false;
  511.         }
  512.           name = strings + sym._n._n_n._n_offset;
  513.         }
  514.       else
  515.         {
  516.           memcpy (buf, sym._n._n_name, SYMNMLEN);
  517.           buf[SYMNMLEN] = '\0';
  518.           name = buf;
  519.           copy = true;
  520.         }
  521.  
  522.       value = sym.n_value;
  523.  
  524.       if (sym.n_scnum == 0)
  525.         {
  526.           if (value == 0)
  527.         {
  528.           flags = 0;
  529.           section = bfd_und_section_ptr;
  530.         }
  531.           else
  532.         {
  533.           flags = BSF_GLOBAL;
  534.           section = bfd_com_section_ptr;
  535.         }
  536.         }
  537.       else
  538.         {
  539.           flags = BSF_EXPORT | BSF_GLOBAL;
  540.           section = coff_section_from_bfd_index (abfd, sym.n_scnum);
  541.           value -= section->vma;
  542.         }
  543.  
  544.       if (! (_bfd_generic_link_add_one_symbol
  545.          (info, abfd, name, flags, section, value,
  546.           (const char *) NULL, copy, false,
  547.           (struct bfd_link_hash_entry **) sym_hash)))
  548.         return false;
  549.  
  550.       if (info->hash->creator->flavour == bfd_get_flavour (abfd))
  551.         {
  552.           if (((*sym_hash)->class == C_NULL
  553.            && (*sym_hash)->type == T_NULL)
  554.           || sym.n_scnum != 0
  555.           || (sym.n_value != 0
  556.               && (*sym_hash)->root.type != bfd_link_hash_defined))
  557.         {
  558.           (*sym_hash)->class = sym.n_sclass;
  559.           (*sym_hash)->type = sym.n_type;
  560.           (*sym_hash)->numaux = sym.n_numaux;
  561.           (*sym_hash)->auxbfd = abfd;
  562.           if (sym.n_numaux != 0)
  563.             {
  564.               union internal_auxent *alloc;
  565.               unsigned int i;
  566.               bfd_byte *eaux;
  567.               union internal_auxent *iaux;
  568.  
  569.               alloc = ((union internal_auxent *)
  570.                    bfd_hash_allocate (&info->hash->table,
  571.                           (sym.n_numaux
  572.                            * sizeof (*alloc))));
  573.               if (alloc == NULL)
  574.             {
  575.               bfd_set_error (bfd_error_no_memory);
  576.               return false;
  577.             }
  578.               for (i = 0, eaux = esym + symesz, iaux = alloc;
  579.                i < sym.n_numaux;
  580.                i++, eaux += symesz, iaux++)
  581.             bfd_coff_swap_aux_in (abfd, (PTR) eaux, sym.n_type,
  582.                           sym.n_sclass, i, sym.n_numaux,
  583.                           (PTR) iaux);
  584.               (*sym_hash)->aux = alloc;
  585.             }
  586.         }
  587.         }
  588.     }
  589.  
  590.       esym += (sym.n_numaux + 1) * symesz;
  591.       sym_hash += sym.n_numaux + 1;
  592.     }
  593.  
  594.   return true;
  595. }
  596.  
  597. /* Do the final link step.  */
  598.  
  599. boolean
  600. _bfd_coff_final_link (abfd, info)
  601.      bfd *abfd;
  602.      struct bfd_link_info *info;
  603. {
  604.   bfd_size_type symesz;
  605.   struct coff_final_link_info finfo;
  606.   asection *o;
  607.   struct bfd_link_order *p;
  608.   size_t max_contents_size;
  609.   size_t max_sym_count;
  610.   size_t max_lineno_count;
  611.   size_t max_reloc_count;
  612.   size_t max_output_reloc_count;
  613.   file_ptr rel_filepos;
  614.   unsigned int relsz;
  615.   file_ptr line_filepos;
  616.   unsigned int linesz;
  617.   bfd *sub;
  618.   bfd_byte *external_relocs = NULL;
  619.   char strbuf[STRING_SIZE_SIZE];
  620.  
  621.   symesz = bfd_coff_symesz (abfd);
  622.  
  623.   finfo.info = info;
  624.   finfo.output_bfd = abfd;
  625.   finfo.strtab = NULL;
  626.   finfo.section_info = NULL;
  627.   finfo.last_file_index = -1;
  628.   finfo.internal_syms = NULL;
  629.   finfo.sec_ptrs = NULL;
  630.   finfo.sym_indices = NULL;
  631.   finfo.outsyms = NULL;
  632.   finfo.linenos = NULL;
  633.   finfo.contents = NULL;
  634.   finfo.external_relocs = NULL;
  635.   finfo.internal_relocs = NULL;
  636.  
  637.   finfo.strtab = _bfd_stringtab_init ();
  638.   if (finfo.strtab == NULL)
  639.     goto error_return;
  640.  
  641.   /* Compute the file positions for all the sections.  */
  642.   if (! abfd->output_has_begun)
  643.     bfd_coff_compute_section_file_positions (abfd);
  644.  
  645.   /* Count the line numbers and relocation entries required for the
  646.      output file.  Set the file positions for the relocs.  */
  647.   rel_filepos = obj_relocbase (abfd);
  648.   relsz = bfd_coff_relsz (abfd);
  649.   max_contents_size = 0;
  650.   max_lineno_count = 0;
  651.   max_reloc_count = 0;
  652.   for (o = abfd->sections; o != NULL; o = o->next)
  653.     {
  654.       o->reloc_count = 0;
  655.       o->lineno_count = 0;
  656.       for (p = o->link_order_head; p != NULL; p = p->next)
  657.     {
  658.       if (p->type == bfd_indirect_link_order)
  659.         {
  660.           asection *sec;
  661.  
  662.           sec = p->u.indirect.section;
  663.  
  664.           if (info->strip == strip_none
  665.           || info->strip == strip_some)
  666.         o->lineno_count += sec->lineno_count;
  667.  
  668.           if (info->relocateable)
  669.         o->reloc_count += sec->reloc_count;
  670.  
  671.           if (sec->_raw_size > max_contents_size)
  672.         max_contents_size = sec->_raw_size;
  673.           if (sec->lineno_count > max_lineno_count)
  674.         max_lineno_count = sec->lineno_count;
  675.           if (sec->reloc_count > max_reloc_count)
  676.         max_reloc_count = sec->reloc_count;
  677.         }
  678.       else if (info->relocateable
  679.            && (p->type == bfd_section_reloc_link_order
  680.                || p->type == bfd_symbol_reloc_link_order))
  681.         ++o->reloc_count;
  682.     }
  683.       if (o->reloc_count == 0)
  684.     o->rel_filepos = 0;
  685.       else
  686.     {
  687.       o->flags |= SEC_RELOC;
  688.       o->rel_filepos = rel_filepos;
  689.       rel_filepos += o->reloc_count * relsz;
  690.     }
  691.     }
  692.  
  693.   /* If doing a relocateable link, allocate space for the pointers we
  694.      need to keep.  */
  695.   if (info->relocateable)
  696.     {
  697.       unsigned int i;
  698.  
  699.       /* We use section_count + 1, rather than section_count, because
  700.          the target_index fields are 1 based.  */
  701.       finfo.section_info = ((struct coff_link_section_info *)
  702.                 malloc ((abfd->section_count + 1)
  703.                     * sizeof (struct coff_link_section_info)));
  704.       if (finfo.section_info == NULL)
  705.     {
  706.       bfd_set_error (bfd_error_no_memory);
  707.       goto error_return;
  708.     }
  709.       for (i = 0; i <= abfd->section_count; i++)
  710.     {
  711.       finfo.section_info[i].relocs = NULL;
  712.       finfo.section_info[i].rel_hashes = NULL;
  713.     }
  714.     }
  715.  
  716.   /* We now know the size of the relocs, so we can determine the file
  717.      positions of the line numbers.  */
  718.   line_filepos = rel_filepos;
  719.   linesz = bfd_coff_linesz (abfd);
  720.   max_output_reloc_count = 0;
  721.   for (o = abfd->sections; o != NULL; o = o->next)
  722.     {
  723.       if (o->lineno_count == 0)
  724.     o->line_filepos = 0;
  725.       else
  726.     {
  727.       o->line_filepos = line_filepos;
  728.       line_filepos += o->lineno_count * linesz;
  729.     }
  730.  
  731.       if (o->reloc_count != 0)
  732.     {
  733.       /* We don't know the indices of global symbols until we have
  734.              written out all the local symbols.  For each section in
  735.              the output file, we keep an array of pointers to hash
  736.              table entries.  Each entry in the array corresponds to a
  737.              reloc.  When we find a reloc against a global symbol, we
  738.              set the corresponding entry in this array so that we can
  739.              fix up the symbol index after we have written out all the
  740.              local symbols.
  741.  
  742.          Because of this problem, we also keep the relocs in
  743.          memory until the end of the link.  This wastes memory,
  744.          but only when doing a relocateable link, which is not the
  745.          common case.  */
  746.       BFD_ASSERT (info->relocateable);
  747.       finfo.section_info[o->target_index].relocs =
  748.         ((struct internal_reloc *)
  749.          malloc (o->reloc_count * sizeof (struct internal_reloc)));
  750.       finfo.section_info[o->target_index].rel_hashes =
  751.         ((struct coff_link_hash_entry **)
  752.          malloc (o->reloc_count
  753.              * sizeof (struct coff_link_hash_entry *)));
  754.       if (finfo.section_info[o->target_index].relocs == NULL
  755.           || finfo.section_info[o->target_index].rel_hashes == NULL)
  756.         {
  757.           bfd_set_error (bfd_error_no_memory);
  758.           goto error_return;
  759.         }
  760.  
  761.       if (o->reloc_count > max_output_reloc_count)
  762.         max_output_reloc_count = o->reloc_count;
  763.     }
  764.  
  765.       /* Reset the reloc and lineno counts, so that we can use them to
  766.      count the number of entries we have output so far.  */
  767.       o->reloc_count = 0;
  768.       o->lineno_count = 0;
  769.     }
  770.  
  771.   obj_sym_filepos (abfd) = line_filepos;
  772.  
  773.   /* Figure out the largest number of symbols in an input BFD.  Take
  774.      the opportunity to clear the output_has_begun fields of all the
  775.      input BFD's.  */
  776.   max_sym_count = 0;
  777.   for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
  778.     {
  779.       size_t sz;
  780.  
  781.       sub->output_has_begun = false;
  782.       sz = obj_raw_syment_count (sub);
  783.       if (sz > max_sym_count)
  784.     max_sym_count = sz;
  785.     }
  786.  
  787.   /* Allocate some buffers used while linking.  */
  788.   finfo.internal_syms = ((struct internal_syment *)
  789.              malloc (max_sym_count
  790.                  * sizeof (struct internal_syment)));
  791.   finfo.sec_ptrs = (asection **) malloc (max_sym_count * sizeof (asection *));
  792.   finfo.sym_indices = (long *) malloc (max_sym_count * sizeof (long));
  793.   finfo.outsyms = (bfd_byte *) malloc ((max_sym_count + 1) * symesz);
  794.   finfo.linenos = (bfd_byte *) malloc (max_lineno_count
  795.                        * bfd_coff_linesz (abfd));
  796.   finfo.contents = (bfd_byte *) malloc (max_contents_size);
  797.   finfo.external_relocs = (bfd_byte *) malloc (max_reloc_count * relsz);
  798.   if (! info->relocateable)
  799.     finfo.internal_relocs = ((struct internal_reloc *)
  800.                  malloc (max_reloc_count
  801.                      * sizeof (struct internal_reloc)));
  802.   if ((finfo.internal_syms == NULL && max_sym_count > 0)
  803.       || (finfo.sec_ptrs == NULL && max_sym_count > 0)
  804.       || (finfo.sym_indices == NULL && max_sym_count > 0)
  805.       || finfo.outsyms == NULL
  806.       || (finfo.linenos == NULL && max_lineno_count > 0)
  807.       || (finfo.contents == NULL && max_contents_size > 0)
  808.       || (finfo.external_relocs == NULL && max_reloc_count > 0)
  809.       || (! info->relocateable
  810.       && finfo.internal_relocs == NULL
  811.       && max_reloc_count > 0))
  812.     {
  813.       bfd_set_error (bfd_error_no_memory);
  814.       goto error_return;
  815.     }
  816.  
  817.   /* We now know the position of everything in the file, except that
  818.      we don't know the size of the symbol table and therefore we don't
  819.      know where the string table starts.  We just build the string
  820.      table in memory as we go along.  We process all the relocations
  821.      for a single input file at once.  */
  822.   obj_raw_syment_count (abfd) = 0;
  823.   for (o = abfd->sections; o != NULL; o = o->next)
  824.     {
  825.       for (p = o->link_order_head; p != NULL; p = p->next)
  826.     {
  827.       if (p->type == bfd_indirect_link_order
  828.           && (bfd_get_flavour (p->u.indirect.section->owner)
  829.           == bfd_target_coff_flavour))
  830.         {
  831.           sub = p->u.indirect.section->owner;
  832.           if (! sub->output_has_begun)
  833.         {
  834.           if (! coff_link_input_bfd (&finfo, sub))
  835.             goto error_return;
  836.           sub->output_has_begun = true;
  837.         }
  838.         }
  839.       else if (p->type == bfd_section_reloc_link_order
  840.            || p->type == bfd_symbol_reloc_link_order)
  841.         {
  842.           if (! coff_reloc_link_order (abfd, &finfo, o, p))
  843.         goto error_return;
  844.         }
  845.       else
  846.         {
  847.           if (! _bfd_default_link_order (abfd, info, o, p))
  848.         goto error_return;
  849.         }
  850.     }
  851.     }
  852.  
  853.   /* Free up the buffers used by coff_link_input_bfd.  */
  854.   if (finfo.internal_syms != NULL)
  855.     {
  856.       free (finfo.internal_syms);
  857.       finfo.internal_syms = NULL;
  858.     }
  859.   if (finfo.sec_ptrs != NULL)
  860.     {
  861.       free (finfo.sec_ptrs);
  862.       finfo.sec_ptrs = NULL;
  863.     }
  864.   if (finfo.sym_indices != NULL)
  865.     {
  866.       free (finfo.sym_indices);
  867.       finfo.sym_indices = NULL;
  868.     }
  869.   if (finfo.linenos != NULL)
  870.     {
  871.       free (finfo.linenos);
  872.       finfo.linenos = NULL;
  873.     }
  874.   if (finfo.contents != NULL)
  875.     {
  876.       free (finfo.contents);
  877.       finfo.contents = NULL;
  878.     }
  879.   if (finfo.external_relocs != NULL)
  880.     {
  881.       free (finfo.external_relocs);
  882.       finfo.external_relocs = NULL;
  883.     }
  884.   if (finfo.internal_relocs != NULL)
  885.     {
  886.       free (finfo.internal_relocs);
  887.       finfo.internal_relocs = NULL;
  888.     }
  889.  
  890.   /* The value of the last C_FILE symbol is supposed to be the symbol
  891.      index of the first external symbol.  Write it out again if
  892.      necessary.  */
  893.   if (finfo.last_file_index != -1
  894.       && finfo.last_file.n_value != obj_raw_syment_count (abfd))
  895.     {
  896.       finfo.last_file.n_value = obj_raw_syment_count (abfd);
  897.       bfd_coff_swap_sym_out (abfd, (PTR) &finfo.last_file,
  898.                  (PTR) finfo.outsyms);
  899.       if (bfd_seek (abfd,
  900.             (obj_sym_filepos (abfd)
  901.              + finfo.last_file_index * symesz),
  902.             SEEK_SET) != 0
  903.       || bfd_write (finfo.outsyms, symesz, 1, abfd) != symesz)
  904.     return false;
  905.     }
  906.  
  907.   /* Write out the global symbols.  */
  908.   finfo.failed = false;
  909.   coff_link_hash_traverse (coff_hash_table (info), coff_write_global_sym,
  910.                (PTR) &finfo);
  911.   if (finfo.failed)
  912.     goto error_return;
  913.  
  914.   /* The outsyms buffer is used by coff_write_global_sym.  */
  915.   if (finfo.outsyms != NULL)
  916.     {
  917.       free (finfo.outsyms);
  918.       finfo.outsyms = NULL;
  919.     }
  920.  
  921.   if (info->relocateable)
  922.     {
  923.       /* Now that we have written out all the global symbols, we know
  924.      the symbol indices to use for relocs against them, and we can
  925.      finally write out the relocs.  */
  926.       external_relocs = (bfd_byte *) malloc (max_output_reloc_count * relsz);
  927.       if (external_relocs == NULL)
  928.     {
  929.       bfd_set_error (bfd_error_no_memory);
  930.       goto error_return;
  931.     }
  932.  
  933.       for (o = abfd->sections; o != NULL; o = o->next)
  934.     {
  935.       struct internal_reloc *irel;
  936.       struct internal_reloc *irelend;
  937.       struct coff_link_hash_entry **rel_hash;
  938.       bfd_byte *erel;
  939.  
  940.       if (o->reloc_count == 0)
  941.         continue;
  942.  
  943.       irel = finfo.section_info[o->target_index].relocs;
  944.       irelend = irel + o->reloc_count;
  945.       rel_hash = finfo.section_info[o->target_index].rel_hashes;
  946.       erel = external_relocs;
  947.       for (; irel < irelend; irel++, rel_hash++, erel += relsz)
  948.         {
  949.           if (*rel_hash != NULL)
  950.         {
  951.           BFD_ASSERT ((*rel_hash)->indx >= 0);
  952.           irel->r_symndx = (*rel_hash)->indx;
  953.         }
  954.           bfd_coff_swap_reloc_out (abfd, (PTR) irel, (PTR) erel);
  955.         }
  956.  
  957.       if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0
  958.           || bfd_write ((PTR) external_relocs, relsz, o->reloc_count,
  959.                 abfd) != relsz * o->reloc_count)
  960.         goto error_return;
  961.     }
  962.  
  963.       free (external_relocs);
  964.       external_relocs = NULL;
  965.     }
  966.  
  967.   /* Free up the section information.  */
  968.   if (finfo.section_info != NULL)
  969.     {
  970.       unsigned int i;
  971.  
  972.       for (i = 0; i < abfd->section_count; i++)
  973.     {
  974.       if (finfo.section_info[i].relocs != NULL)
  975.         free (finfo.section_info[i].relocs);
  976.       if (finfo.section_info[i].rel_hashes != NULL)
  977.         free (finfo.section_info[i].rel_hashes);
  978.     }
  979.       free (finfo.section_info);
  980.       finfo.section_info = NULL;
  981.     }
  982.  
  983.   /* Write out the string table.  */
  984.   if (bfd_seek (abfd,
  985.         (obj_sym_filepos (abfd)
  986.          + obj_raw_syment_count (abfd) * symesz),
  987.         SEEK_SET) != 0)
  988.     return false;
  989.  
  990. #if STRING_SIZE_SIZE == 4
  991.   bfd_h_put_32 (abfd,
  992.         _bfd_stringtab_size (finfo.strtab) + STRING_SIZE_SIZE,
  993.         (bfd_byte *) strbuf);
  994. #else
  995.  #error Change bfd_h_put_32
  996. #endif
  997.  
  998.   if (bfd_write (strbuf, 1, STRING_SIZE_SIZE, abfd) != STRING_SIZE_SIZE)
  999.     return false;
  1000.  
  1001.   if (! _bfd_stringtab_emit (abfd, finfo.strtab))
  1002.     return false;
  1003.  
  1004.   _bfd_stringtab_free (finfo.strtab);
  1005.  
  1006.   /* Setting bfd_get_symcount to 0 will cause write_object_contents to
  1007.      not try to write out the symbols.  */
  1008.   bfd_get_symcount (abfd) = 0;
  1009.  
  1010.   return true;
  1011.  
  1012.  error_return:
  1013.   if (finfo.strtab != NULL)
  1014.     _bfd_stringtab_free (finfo.strtab);
  1015.   if (finfo.section_info != NULL)
  1016.     {
  1017.       unsigned int i;
  1018.  
  1019.       for (i = 0; i < abfd->section_count; i++)
  1020.     {
  1021.       if (finfo.section_info[i].relocs != NULL)
  1022.         free (finfo.section_info[i].relocs);
  1023.       if (finfo.section_info[i].rel_hashes != NULL)
  1024.         free (finfo.section_info[i].rel_hashes);
  1025.     }
  1026.       free (finfo.section_info);
  1027.     }
  1028.   if (finfo.internal_syms != NULL)
  1029.     free (finfo.internal_syms);
  1030.   if (finfo.sec_ptrs != NULL)
  1031.     free (finfo.sec_ptrs);
  1032.   if (finfo.sym_indices != NULL)
  1033.     free (finfo.sym_indices);
  1034.   if (finfo.outsyms != NULL)
  1035.     free (finfo.outsyms);
  1036.   if (finfo.linenos != NULL)
  1037.     free (finfo.linenos);
  1038.   if (finfo.contents != NULL)
  1039.     free (finfo.contents);
  1040.   if (finfo.external_relocs != NULL)
  1041.     free (finfo.external_relocs);
  1042.   if (finfo.internal_relocs != NULL)
  1043.     free (finfo.internal_relocs);
  1044.   if (external_relocs != NULL)
  1045.     free (external_relocs);
  1046.   return false;
  1047. }
  1048.  
  1049. /* Link an input file into the linker output file.  This function
  1050.    handles all the sections and relocations of the input file at once.  */
  1051.  
  1052. static boolean
  1053. coff_link_input_bfd (finfo, input_bfd)
  1054.      struct coff_final_link_info *finfo;
  1055.      bfd *input_bfd;
  1056. {
  1057.   boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
  1058.   bfd *output_bfd;
  1059.   const char *strings;
  1060.   bfd_size_type syment_base;
  1061.   unsigned int n_tmask;
  1062.   unsigned int n_btshft;
  1063.   boolean copy, hash;
  1064.   bfd_size_type isymesz;
  1065.   bfd_size_type osymesz;
  1066.   bfd_size_type linesz;
  1067.   bfd_byte *esym;
  1068.   bfd_byte *esym_end;
  1069.   struct internal_syment *isymp;
  1070.   asection **secpp;
  1071.   long *indexp;
  1072.   long output_index;
  1073.   bfd_byte *outsym;
  1074.   struct coff_link_hash_entry **sym_hash;
  1075.   bfd_size_type relsz;
  1076.   asection *o;
  1077.  
  1078.   /* Move all the symbols to the output file.  */
  1079.  
  1080.   output_bfd = finfo->output_bfd;
  1081.   sym_is_global = coff_backend_info (input_bfd)->_bfd_coff_sym_is_global;
  1082.   strings = NULL;
  1083.   syment_base = obj_raw_syment_count (output_bfd);
  1084.   isymesz = bfd_coff_symesz (input_bfd);
  1085.   osymesz = bfd_coff_symesz (output_bfd);
  1086.   linesz = bfd_coff_linesz (input_bfd);
  1087.   BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
  1088.  
  1089.   n_tmask = coff_data (input_bfd)->local_n_tmask;
  1090.   n_btshft = coff_data (input_bfd)->local_n_btshft;
  1091.  
  1092.   /* Define macros so that ISFCN, et. al., macros work correctly.  */
  1093. #define N_TMASK n_tmask
  1094. #define N_BTSHFT n_btshft
  1095.  
  1096.   copy = false;
  1097.   if (! finfo->info->keep_memory)
  1098.     copy = true;
  1099.   hash = true;
  1100.   if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
  1101.     hash = false;
  1102.  
  1103.   if (! coff_link_get_symbols (input_bfd))
  1104.     return false;
  1105.  
  1106.   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
  1107.   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
  1108.   isymp = finfo->internal_syms;
  1109.   secpp = finfo->sec_ptrs;
  1110.   indexp = finfo->sym_indices;
  1111.   output_index = syment_base;
  1112.   outsym = finfo->outsyms;
  1113.   while (esym < esym_end)
  1114.     {
  1115.       struct internal_syment isym;
  1116.       boolean skip;
  1117.       boolean global;
  1118.       int add;
  1119.  
  1120.       bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
  1121.  
  1122.       /* Make a copy of *isymp so that the relocate_section function
  1123.      always sees the original values.  This is more reliable than
  1124.      always recomputing the symbol value even if we are stripping
  1125.      the symbol.  */
  1126.       isym = *isymp;
  1127.  
  1128.       if (isym.n_scnum != 0)
  1129.     *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
  1130.       else
  1131.     {
  1132.       if (isym.n_value == 0)
  1133.         *secpp = bfd_und_section_ptr;
  1134.       else
  1135.         *secpp = bfd_com_section_ptr;
  1136.     }
  1137.  
  1138.       *indexp = -1;
  1139.  
  1140.       skip = false;
  1141.       global = false;
  1142.       add = 1 + isym.n_numaux;
  1143.  
  1144.       /* If we are stripping all symbols, we want to skip this one.  */
  1145.       if (finfo->info->strip == strip_all)
  1146.     skip = true;
  1147.  
  1148.       if (! skip)
  1149.     {
  1150.       if (isym.n_sclass == C_EXT
  1151.           || (sym_is_global && (*sym_is_global) (input_bfd, &isym)))
  1152.         {
  1153.           /* This is a global symbol.  Global symbols come at the
  1154.          end of the symbol table, so skip them for now.
  1155.          Function symbols, however, are an exception, and are
  1156.          not moved to the end.  */
  1157.           global = true;
  1158.           if (! ISFCN (isym.n_type))
  1159.         skip = true;
  1160.         }
  1161.       else
  1162.         {
  1163.           /* This is a local symbol.  Skip it if we are discarding
  1164.                  local symbols.  */
  1165.           if (finfo->info->discard == discard_all)
  1166.         skip = true;
  1167.         }
  1168.     }
  1169.  
  1170.       /* If we stripping debugging symbols, and this is a debugging
  1171.          symbol, then skip it.  */
  1172.       if (! skip
  1173.       && finfo->info->strip == strip_debugger
  1174.       && isym.n_scnum == N_DEBUG)
  1175.     skip = true;
  1176.  
  1177.       /* If some symbols are stripped based on the name, work out the
  1178.      name and decide whether to skip this symbol.  */
  1179.       if (! skip
  1180.       && (finfo->info->strip == strip_some
  1181.           || finfo->info->discard == discard_l))
  1182.     {
  1183.       const char *name;
  1184.       char buf[SYMNMLEN + 1];
  1185.  
  1186.       if (isym._n._n_n._n_zeroes == 0
  1187.           && isym._n._n_n._n_offset != 0)
  1188.         {
  1189.           if (strings == NULL)
  1190.         {
  1191.           strings = coff_read_string_table (input_bfd);
  1192.           if (strings == NULL)
  1193.             return false;
  1194.         }
  1195.           name = strings + isym._n._n_n._n_offset;
  1196.         }
  1197.       else
  1198.         {
  1199.           memcpy (buf, isym._n._n_name, SYMNMLEN);
  1200.           buf[SYMNMLEN] = '\0';
  1201.           name = buf;
  1202.         }
  1203.  
  1204.       if ((finfo->info->strip == strip_some
  1205.            && (bfd_hash_lookup (finfo->info->keep_hash, name, false,
  1206.                     false) == NULL))
  1207.           || (! global
  1208.           && finfo->info->discard == discard_l
  1209.           && strncmp (name, finfo->info->lprefix,
  1210.                   finfo->info->lprefix_len) == 0))
  1211.         skip = true;
  1212.     }
  1213.  
  1214.       /* We now know whether we are to skip this symbol or not.  */
  1215.       if (! skip)
  1216.     {
  1217.       /* Adjust the symbol in order to output it.  */
  1218.  
  1219.       if (isym._n._n_n._n_zeroes == 0
  1220.           && isym._n._n_n._n_offset != 0)
  1221.         {
  1222.           const char *name;
  1223.           bfd_size_type indx;
  1224.  
  1225.           /* This symbol has a long name.  Enter it in the string
  1226.          table we are building.  Note that we do not check
  1227.          bfd_coff_symname_in_debug.  That is only true for
  1228.          XCOFF, and XCOFF requires different linking code
  1229.          anyhow.  */
  1230.           BFD_ASSERT (isym._n._n_n._n_offset >= STRING_SIZE_SIZE);
  1231.           if (strings == NULL)
  1232.         {
  1233.           strings = coff_read_string_table (input_bfd);
  1234.           if (strings == NULL)
  1235.             return false;
  1236.         }
  1237.           name = strings + isym._n._n_n._n_offset;
  1238.           indx = _bfd_stringtab_add (finfo->strtab, name, hash, copy);
  1239.           if (indx == (bfd_size_type) -1)
  1240.         return false;
  1241.           isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  1242.         }
  1243.  
  1244.       if (isym.n_scnum > 0)
  1245.         {
  1246.           isym.n_scnum = (*secpp)->output_section->target_index;
  1247.           isym.n_value += ((*secpp)->output_section->vma
  1248.                    + (*secpp)->output_offset
  1249.                    - (*secpp)->vma);
  1250.         }
  1251.  
  1252.       /* The value of a C_FILE symbol is the symbol index of the
  1253.          next C_FILE symbol.  The value of the last C_FILE symbol
  1254.          is the symbol index to the first external symbol
  1255.          (actually, coff_renumber_symbols does not get this
  1256.          right--it just sets the value of the last C_FILE symbol
  1257.          to zero--and nobody has ever complained about it).  We
  1258.          try to get this right, below, just before we write the
  1259.          symbols out, but in the general case we may have to write
  1260.          the symbol out twice.  */
  1261.       if (isym.n_sclass == C_FILE)
  1262.         {
  1263.           if (finfo->last_file_index != -1
  1264.           && finfo->last_file.n_value != output_index)
  1265.         {
  1266.           /* We must correct the value of the last C_FILE entry.  */
  1267.           finfo->last_file.n_value = output_index;
  1268.           if (finfo->last_file_index >= syment_base)
  1269.             {
  1270.               /* The last C_FILE symbol is in this input file.  */
  1271.               bfd_coff_swap_sym_out (output_bfd,
  1272.                          (PTR) &finfo->last_file,
  1273.                          (PTR) (finfo->outsyms
  1274.                             + ((finfo->last_file_index
  1275.                             - syment_base)
  1276.                                * osymesz)));
  1277.             }
  1278.           else
  1279.             {
  1280.               /* We have already written out the last C_FILE
  1281.              symbol.  We need to write it out again.  We
  1282.              borrow *outsym temporarily.  */
  1283.               bfd_coff_swap_sym_out (output_bfd,
  1284.                          (PTR) &finfo->last_file,
  1285.                          (PTR) outsym);
  1286.               if (bfd_seek (output_bfd,
  1287.                     (obj_sym_filepos (output_bfd)
  1288.                      + finfo->last_file_index * osymesz),
  1289.                     SEEK_SET) != 0
  1290.               || (bfd_write (outsym, osymesz, 1, output_bfd)
  1291.                   != osymesz))
  1292.             return false;
  1293.             }
  1294.         }
  1295.  
  1296.           finfo->last_file_index = output_index;
  1297.           finfo->last_file = isym;
  1298.         }
  1299.  
  1300.       /* Output the symbol.  */
  1301.  
  1302.       bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) outsym);
  1303.  
  1304.       *indexp = output_index;
  1305.  
  1306.       if (global)
  1307.         {
  1308.           long indx;
  1309.           struct coff_link_hash_entry *h;
  1310.  
  1311.           indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
  1312.               / isymesz);
  1313.           h = obj_coff_sym_hashes (input_bfd)[indx];
  1314.           BFD_ASSERT (h != NULL);
  1315.           h->indx = output_index;
  1316.         }
  1317.  
  1318.       output_index += add;
  1319.       outsym += add * osymesz;
  1320.     }
  1321.  
  1322.       esym += add * isymesz;
  1323.       isymp += add;
  1324.       ++secpp;
  1325.       ++indexp;
  1326.       for (--add; add > 0; --add)
  1327.     {
  1328.       *secpp++ = NULL;
  1329.       *indexp++ = -1;
  1330.     }
  1331.     }
  1332.  
  1333.   /* Fix up the aux entries.  This must be done in a separate pass,
  1334.      because we don't know the correct symbol indices until we have
  1335.      already decided which symbols we are going to keep.  */
  1336.  
  1337.   esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
  1338.   esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
  1339.   isymp = finfo->internal_syms;
  1340.   indexp = finfo->sym_indices;
  1341.   sym_hash = obj_coff_sym_hashes (input_bfd);
  1342.   outsym = finfo->outsyms;
  1343.   while (esym < esym_end)
  1344.     {
  1345.       int add;
  1346.  
  1347.       add = 1 + isymp->n_numaux;
  1348.  
  1349.       if (*indexp < 0
  1350.       && (*sym_hash == NULL
  1351.           || (*sym_hash)->auxbfd != input_bfd))
  1352.     esym += add * isymesz;
  1353.       else
  1354.     {
  1355.       struct coff_link_hash_entry *h;
  1356.       int i;
  1357.  
  1358.       h = NULL;
  1359.       if (*indexp < 0)
  1360.         {
  1361.           h = *sym_hash;
  1362.           BFD_ASSERT (h->numaux == isymp->n_numaux);
  1363.         }
  1364.  
  1365.       esym += isymesz;
  1366.  
  1367.       if (h == NULL)
  1368.         outsym += osymesz;
  1369.  
  1370.       /* Handle the aux entries.  This handling is based on
  1371.          coff_pointerize_aux.  I don't know if it always correct.  */
  1372.       for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
  1373.         {
  1374.           union internal_auxent aux;
  1375.           union internal_auxent *auxp;
  1376.  
  1377.           if (h != NULL)
  1378.         auxp = h->aux + i;
  1379.           else
  1380.         {
  1381.           bfd_coff_swap_aux_in (input_bfd, (PTR) esym, isymp->n_type,
  1382.                     isymp->n_sclass, i, isymp->n_numaux,
  1383.                     (PTR) &aux);
  1384.           auxp = &aux;
  1385.         }
  1386.  
  1387.           if (isymp->n_sclass == C_FILE)
  1388.         {
  1389.           /* If this is a long filename, we must put it in the
  1390.              string table.  */
  1391.           if (auxp->x_file.x_n.x_zeroes == 0
  1392.               && auxp->x_file.x_n.x_offset != 0)
  1393.             {
  1394.               const char *filename;
  1395.               bfd_size_type indx;
  1396.  
  1397.               BFD_ASSERT (auxp->x_file.x_n.x_offset
  1398.                   >= STRING_SIZE_SIZE);
  1399.               if (strings == NULL)
  1400.             {
  1401.               strings = coff_read_string_table (input_bfd);
  1402.               if (strings == NULL)
  1403.                 return false;
  1404.             }
  1405.               filename = strings + auxp->x_file.x_n.x_offset;
  1406.               indx = _bfd_stringtab_add (finfo->strtab, filename,
  1407.                          hash, copy);
  1408.               if (indx == (bfd_size_type) -1)
  1409.             return false;
  1410.               auxp->x_file.x_n.x_offset = STRING_SIZE_SIZE + indx;
  1411.             }
  1412.         }
  1413.           else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
  1414.         {
  1415.           long indx;
  1416.  
  1417.           if (ISFCN (isymp->n_type)
  1418.               || ISTAG (isymp->n_sclass)
  1419.               || isymp->n_sclass == C_BLOCK)
  1420.             {
  1421.               indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
  1422.               if (indx > 0
  1423.               && indx < obj_raw_syment_count (input_bfd))
  1424.             {
  1425.               /* We look forward through the symbol for
  1426.                              the index of the next symbol we are going
  1427.                              to include.  I don't know if this is
  1428.                              entirely right.  */
  1429.               while (finfo->sym_indices[indx] < 0
  1430.                  && indx < obj_raw_syment_count (input_bfd))
  1431.                 ++indx;
  1432.               if (indx >= obj_raw_syment_count (input_bfd))
  1433.                 indx = output_index;
  1434.               else
  1435.                 indx = finfo->sym_indices[indx];
  1436.               auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
  1437.             }
  1438.             }
  1439.  
  1440.           indx = auxp->x_sym.x_tagndx.l;
  1441.           if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
  1442.             {
  1443.               indx = finfo->sym_indices[indx];
  1444.               if (indx < 0)
  1445.             auxp->x_sym.x_tagndx.l = 0;
  1446.               else
  1447.             auxp->x_sym.x_tagndx.l = indx;
  1448.             }
  1449.         }
  1450.  
  1451.           if (h == NULL)
  1452.         {
  1453.           bfd_coff_swap_aux_out (output_bfd, (PTR) auxp, isymp->n_type,
  1454.                      isymp->n_sclass, i, isymp->n_numaux,
  1455.                      (PTR) outsym);
  1456.           outsym += osymesz;
  1457.         }
  1458.  
  1459.           esym += isymesz;
  1460.         }
  1461.     }
  1462.  
  1463.       indexp += add;
  1464.       isymp += add;
  1465.       sym_hash += add;
  1466.     }
  1467.  
  1468.   /* Relocate the line numbers, unless we are stripping them.  */
  1469.   if (finfo->info->strip == strip_none
  1470.       || finfo->info->strip == strip_some)
  1471.     {
  1472.       for (o = input_bfd->sections; o != NULL; o = o->next)
  1473.     {
  1474.       bfd_vma offset;
  1475.       bfd_byte *eline;
  1476.       bfd_byte *elineend;
  1477.  
  1478.       if (o->lineno_count == 0)
  1479.         continue;
  1480.  
  1481.       if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
  1482.           || bfd_read (finfo->linenos, linesz, o->lineno_count,
  1483.                input_bfd) != linesz * o->lineno_count)
  1484.         return false;
  1485.  
  1486.       offset = o->output_section->vma + o->output_offset - o->vma;
  1487.       eline = finfo->linenos;
  1488.       elineend = eline + linesz * o->lineno_count;
  1489.       for (; eline < elineend; eline += linesz)
  1490.         {
  1491.           struct internal_lineno iline;
  1492.  
  1493.           bfd_coff_swap_lineno_in (input_bfd, (PTR) eline, (PTR) &iline);
  1494.  
  1495.           if (iline.l_lnno != 0)
  1496.         iline.l_addr.l_paddr += offset;
  1497.           else if (iline.l_addr.l_symndx >= 0
  1498.                && (iline.l_addr.l_symndx
  1499.                < obj_raw_syment_count (input_bfd)))
  1500.         {
  1501.           long indx;
  1502.  
  1503.           indx = finfo->sym_indices[iline.l_addr.l_symndx];
  1504.  
  1505.           if (indx < 0)
  1506.             {
  1507.               /* These line numbers are attached to a symbol
  1508.              which we are stripping.  We should really
  1509.              just discard the line numbers, but that would
  1510.              be a pain because we have already counted
  1511.              them.  */
  1512.               indx = 0;
  1513.             }
  1514.           else
  1515.             {
  1516.               struct internal_syment is;
  1517.               union internal_auxent ia;
  1518.  
  1519.               /* Fix up the lnnoptr field in the aux entry of
  1520.              the symbol.  It turns out that we can't do
  1521.              this when we modify the symbol aux entries,
  1522.              because gas sometimes screws up the lnnoptr
  1523.              field and makes it an offset from the start
  1524.              of the line numbers rather than an absolute
  1525.              file index.  */
  1526.               bfd_coff_swap_sym_in (output_bfd,
  1527.                         (PTR) (finfo->outsyms
  1528.                            + ((indx - syment_base)
  1529.                               * osymesz)),
  1530.                         (PTR) &is);
  1531.               if ((ISFCN (is.n_type)
  1532.                || is.n_sclass == C_BLOCK)
  1533.               && is.n_numaux >= 1)
  1534.             {
  1535.               PTR auxptr;
  1536.  
  1537.               auxptr = (PTR) (finfo->outsyms
  1538.                       + ((indx - syment_base + 1)
  1539.                          * osymesz));
  1540.               bfd_coff_swap_aux_in (output_bfd, auxptr,
  1541.                         is.n_type, is.n_sclass,
  1542.                         0, is.n_numaux, (PTR) &ia);
  1543.               ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
  1544.                 (o->output_section->line_filepos
  1545.                  + o->output_section->lineno_count * linesz
  1546.                  + eline - finfo->linenos);
  1547.               bfd_coff_swap_aux_out (output_bfd, (PTR) &ia,
  1548.                          is.n_type, is.n_sclass, 0,
  1549.                          is.n_numaux, auxptr);
  1550.             }
  1551.             }
  1552.  
  1553.           iline.l_addr.l_symndx = indx;
  1554.         }
  1555.  
  1556.           bfd_coff_swap_lineno_out (output_bfd, (PTR) &iline, (PTR) eline);
  1557.         }
  1558.  
  1559.       if (bfd_seek (output_bfd,
  1560.             (o->output_section->line_filepos
  1561.              + o->output_section->lineno_count * linesz),
  1562.             SEEK_SET) != 0
  1563.           || bfd_write (finfo->linenos, linesz, o->lineno_count,
  1564.                 output_bfd) != linesz * o->lineno_count)
  1565.         return false;
  1566.  
  1567.       o->output_section->lineno_count += o->lineno_count;
  1568.     }
  1569.     }
  1570.  
  1571.   /* If we swapped out a C_FILE symbol, guess that the next C_FILE
  1572.      symbol will be the first symbol in the next input file.  In the
  1573.      normal case, this will save us from writing out the C_FILE symbol
  1574.      again.  */
  1575.   if (finfo->last_file_index != -1
  1576.       && finfo->last_file_index >= syment_base)
  1577.     {
  1578.       finfo->last_file.n_value = output_index;
  1579.       bfd_coff_swap_sym_out (output_bfd, (PTR) &finfo->last_file,
  1580.                  (PTR) (finfo->outsyms
  1581.                      + ((finfo->last_file_index - syment_base)
  1582.                         * osymesz)));
  1583.     }
  1584.  
  1585.   /* Write the modified symbols to the output file.  */
  1586.   if (outsym > finfo->outsyms)
  1587.     {
  1588.       if (bfd_seek (output_bfd,
  1589.             obj_sym_filepos (output_bfd) + syment_base * osymesz,
  1590.             SEEK_SET) != 0
  1591.       || bfd_write (finfo->outsyms, outsym - finfo->outsyms, 1,
  1592.             output_bfd) != outsym - finfo->outsyms)
  1593.     return false;
  1594.  
  1595.       BFD_ASSERT ((obj_raw_syment_count (output_bfd)
  1596.            + (outsym - finfo->outsyms) / osymesz)
  1597.           == output_index);
  1598.  
  1599.       obj_raw_syment_count (output_bfd) = output_index;
  1600.     }
  1601.  
  1602.   /* Relocate the contents of each section.  */
  1603.   relsz = bfd_coff_relsz (input_bfd);
  1604.   for (o = input_bfd->sections; o != NULL; o = o->next)
  1605.     {
  1606.       if ((o->flags & SEC_HAS_CONTENTS) == 0)
  1607.     continue;
  1608.  
  1609.       if (! bfd_get_section_contents (input_bfd, o, finfo->contents,
  1610.                       (file_ptr) 0, o->_raw_size))
  1611.     return false;
  1612.  
  1613.       if ((o->flags & SEC_RELOC) != 0)
  1614.     {
  1615.       int target_index;
  1616.       struct internal_reloc *internal_relocs;
  1617.       bfd_byte *erel;
  1618.       bfd_byte *erel_end;
  1619.       struct internal_reloc *irel;
  1620.  
  1621.       /* Read in the relocs.  */
  1622.       if (bfd_seek (input_bfd, o->rel_filepos, SEEK_SET) != 0
  1623.           || (bfd_read (finfo->external_relocs, relsz, o->reloc_count,
  1624.                 input_bfd) != relsz * o->reloc_count))
  1625.         return false;
  1626.  
  1627.       /* If we are doing a relocateable link, we keep the swapped
  1628.          in relocs in memory, and don't write them out until the
  1629.          end of the link.  */
  1630.       target_index = o->output_section->target_index;
  1631.       if (! finfo->info->relocateable)
  1632.         internal_relocs = finfo->internal_relocs;
  1633.       else
  1634.         internal_relocs = (finfo->section_info[target_index].relocs
  1635.                    + o->output_section->reloc_count);
  1636.  
  1637.       /* Swap in the relocs.  */
  1638.       erel = finfo->external_relocs;
  1639.       erel_end = erel + relsz * o->reloc_count;
  1640.       irel = internal_relocs;
  1641.       for (; erel < erel_end; erel += relsz, irel++)
  1642.         bfd_coff_swap_reloc_in (input_bfd, (PTR) erel, (PTR) irel);
  1643.  
  1644.       /* Call processor specific code to relocate the section
  1645.              contents.  */
  1646.       if (! bfd_coff_relocate_section (output_bfd, finfo->info,
  1647.                        input_bfd, o,
  1648.                        finfo->contents,
  1649.                        internal_relocs,
  1650.                        finfo->internal_syms,
  1651.                        finfo->sec_ptrs))
  1652.         return false;
  1653.  
  1654.       if (finfo->info->relocateable)
  1655.         {
  1656.           bfd_vma offset;
  1657.           struct internal_reloc *irelend;
  1658.           struct coff_link_hash_entry **rel_hash;
  1659.  
  1660.           offset = o->output_section->vma + o->output_offset - o->vma;
  1661.  
  1662.           irel = internal_relocs;
  1663.           irelend = irel + o->reloc_count;
  1664.           rel_hash = (finfo->section_info[target_index].rel_hashes
  1665.               + o->output_section->reloc_count);
  1666.           for (; irel < irelend; irel++, rel_hash++)
  1667.         {
  1668.           struct coff_link_hash_entry *h;
  1669.  
  1670.           *rel_hash = NULL;
  1671.  
  1672.           /* Adjust the reloc address and symbol index.  */
  1673.  
  1674.           irel->r_vaddr += offset;
  1675.  
  1676.           if (irel->r_symndx == -1)
  1677.             continue;
  1678.  
  1679.           h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
  1680.           if (h != NULL)
  1681.             {
  1682.               /* This is a global symbol.  */
  1683.               if (h->indx >= 0)
  1684.             irel->r_symndx = h->indx;
  1685.               else
  1686.             {
  1687.               /* This symbol is being written at the end
  1688.                  of the file, and we do not yet know the
  1689.                  symbol index.  We save the pointer to the
  1690.                  hash table entry in the rel_hash list.
  1691.                  We set the indx field to -2 to indicate
  1692.                  that this symbol must not be stripped.  */
  1693.               *rel_hash = h;
  1694.               h->indx = -2;
  1695.             }
  1696.             }
  1697.           else
  1698.             {
  1699.               long indx;
  1700.  
  1701.               indx = finfo->sym_indices[irel->r_symndx];
  1702.               if (indx != -1)
  1703.             irel->r_symndx = indx;
  1704.               else
  1705.             {
  1706.               struct internal_syment *is;
  1707.               const char *name;
  1708.               char buf[SYMNMLEN + 1];
  1709.  
  1710.               /* This reloc is against a symbol we are
  1711.                              stripping.  It would be possible to
  1712.                              handle this case, but I don't think it's
  1713.                              worth it.  */
  1714.               is = finfo->internal_syms + irel->r_symndx;
  1715.  
  1716.               if (is->_n._n_n._n_zeroes == 0
  1717.                   && is->_n._n_n._n_offset != 0)
  1718.                 {
  1719.                   if (strings == NULL)
  1720.                 {
  1721.                   strings = coff_read_string_table (input_bfd);
  1722.                   if (strings == NULL)
  1723.                     return false;
  1724.                 }
  1725.                   name = strings + is->_n._n_n._n_offset;
  1726.                 }
  1727.               else
  1728.                 {
  1729.                   memcpy (buf, is->_n._n_name, SYMNMLEN);
  1730.                   buf[SYMNMLEN] = '\0';
  1731.                   name = buf;
  1732.                 }
  1733.  
  1734.               if (! ((*finfo->info->callbacks->unattached_reloc)
  1735.                  (finfo->info, name, input_bfd, o,
  1736.                   irel->r_vaddr)))
  1737.                 return false;
  1738.             }
  1739.             }
  1740.         }
  1741.  
  1742.           o->output_section->reloc_count += o->reloc_count;
  1743.         }
  1744.     }
  1745.  
  1746.       /* Write out the modified section contents.  */
  1747.       if (! bfd_set_section_contents (output_bfd, o->output_section,
  1748.                       finfo->contents, o->output_offset,
  1749.                       (o->_cooked_size != 0
  1750.                        ? o->_cooked_size
  1751.                        : o->_raw_size)))
  1752.     return false;
  1753.     }
  1754.  
  1755.   if (! finfo->info->keep_memory)
  1756.     {
  1757.       if (! coff_link_free_symbols (input_bfd))
  1758.     return false;
  1759.     }
  1760.  
  1761.   return true;
  1762. }
  1763.  
  1764. /* Write out a global symbol.  Called via coff_link_hash_traverse.  */
  1765.  
  1766. static boolean
  1767. coff_write_global_sym (h, data)
  1768.      struct coff_link_hash_entry *h;
  1769.      PTR data;
  1770. {
  1771.   struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
  1772.   bfd *output_bfd;
  1773.   struct internal_syment isym;
  1774.   bfd_size_type symesz;
  1775.   unsigned int i;
  1776.  
  1777.   output_bfd = finfo->output_bfd;
  1778.  
  1779.   if (h->indx >= 0)
  1780.     return true;
  1781.  
  1782.   if (h->indx != -2
  1783.       && (finfo->info->strip == strip_all
  1784.       || (finfo->info->strip == strip_some
  1785.           && (bfd_hash_lookup (finfo->info->keep_hash,
  1786.                    h->root.root.string, false, false)
  1787.           == NULL))))
  1788.     return true;
  1789.  
  1790.   switch (h->root.type)
  1791.     {
  1792.     default:
  1793.     case bfd_link_hash_new:
  1794.       abort ();
  1795.       return false;
  1796.  
  1797.     case bfd_link_hash_undefined:
  1798.     case bfd_link_hash_weak:
  1799.       isym.n_scnum = N_UNDEF;
  1800.       isym.n_value = 0;
  1801.       break;
  1802.  
  1803.     case bfd_link_hash_defined:
  1804.       {
  1805.     asection *sec;
  1806.  
  1807.     sec = h->root.u.def.section->output_section;
  1808.     if (bfd_is_abs_section (sec))
  1809.       isym.n_scnum = N_ABS;
  1810.     else
  1811.       isym.n_scnum = sec->target_index;
  1812.     isym.n_value = (h->root.u.def.value
  1813.             + sec->vma
  1814.             + h->root.u.def.section->output_offset);
  1815.       }
  1816.       break;
  1817.  
  1818.     case bfd_link_hash_common:
  1819.       isym.n_scnum = N_UNDEF;
  1820.       isym.n_value = h->root.u.c.size;
  1821.       break;
  1822.  
  1823.     case bfd_link_hash_indirect:
  1824.     case bfd_link_hash_warning:
  1825.       /* Just ignore these.  They can't be handled anyhow.  */
  1826.       return true;
  1827.     }
  1828.  
  1829.   if (strlen (h->root.root.string) <= SYMNMLEN)
  1830.     strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
  1831.   else
  1832.     {
  1833.       boolean hash;
  1834.       bfd_size_type indx;
  1835.  
  1836.       hash = true;
  1837.       if ((output_bfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
  1838.     hash = false;
  1839.       indx = _bfd_stringtab_add (finfo->strtab, h->root.root.string, hash,
  1840.                  false);
  1841.       if (indx == (bfd_size_type) -1)
  1842.     {
  1843.       finfo->failed = true;
  1844.       return false;
  1845.     }
  1846.       isym._n._n_n._n_zeroes = 0;
  1847.       isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  1848.     }
  1849.  
  1850.   isym.n_sclass = h->class;
  1851.   isym.n_type = h->type;
  1852.  
  1853.   if (isym.n_sclass == C_NULL)
  1854.     isym.n_sclass = C_EXT;
  1855.  
  1856.   isym.n_numaux = h->numaux;
  1857.   
  1858.   bfd_coff_swap_sym_out (output_bfd, (PTR) &isym, (PTR) finfo->outsyms);
  1859.  
  1860.   symesz = bfd_coff_symesz (output_bfd);
  1861.  
  1862.   if (bfd_seek (output_bfd,
  1863.         (obj_sym_filepos (output_bfd)
  1864.          + obj_raw_syment_count (output_bfd) * symesz),
  1865.         SEEK_SET) != 0
  1866.       || bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
  1867.     {
  1868.       finfo->failed = true;
  1869.       return false;
  1870.     }
  1871.  
  1872.   h->indx = obj_raw_syment_count (output_bfd);
  1873.  
  1874.   ++obj_raw_syment_count (output_bfd);
  1875.  
  1876.   /* Write out any associated aux entries.  There normally will be
  1877.      none.  If there are any, I have no idea how to modify them.  */
  1878.   for (i = 0; i < isym.n_numaux; i++)
  1879.     {
  1880.       bfd_coff_swap_aux_out (output_bfd, (PTR) (h->aux + i), isym.n_type,
  1881.                  isym.n_sclass, i, isym.n_numaux,
  1882.                  (PTR) finfo->outsyms);
  1883.       if (bfd_write (finfo->outsyms, symesz, 1, output_bfd) != symesz)
  1884.     {
  1885.       finfo->failed = true;
  1886.       return false;
  1887.     }
  1888.       ++obj_raw_syment_count (output_bfd);
  1889.     }
  1890.  
  1891.   return true;
  1892. }
  1893.  
  1894. /* Handle a link order which is supposed to generate a reloc.  */
  1895.  
  1896. static boolean
  1897. coff_reloc_link_order (output_bfd, finfo, output_section, link_order)
  1898.      bfd *output_bfd;
  1899.      struct coff_final_link_info *finfo;
  1900.      asection *output_section;
  1901.      struct bfd_link_order *link_order;
  1902. {
  1903.   const reloc_howto_type *howto;
  1904.   struct internal_reloc *irel;
  1905.   struct coff_link_hash_entry **rel_hash_ptr;
  1906.  
  1907.   howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  1908.   if (howto == NULL)
  1909.     {
  1910.       bfd_set_error (bfd_error_bad_value);
  1911.       return false;
  1912.     }
  1913.  
  1914.   if (link_order->u.reloc.p->addend != 0)
  1915.     {
  1916.       bfd_size_type size;
  1917.       bfd_byte *buf;
  1918.       bfd_reloc_status_type rstat;
  1919.       boolean ok;
  1920.  
  1921.       size = bfd_get_reloc_size (howto);
  1922.       buf = (bfd_byte *) bfd_zmalloc (size);
  1923.       if (buf == NULL)
  1924.     {
  1925.       bfd_set_error (bfd_error_no_memory);
  1926.       return false;
  1927.     }
  1928.  
  1929.       rstat = _bfd_relocate_contents (howto, output_bfd,
  1930.                       link_order->u.reloc.p->addend, buf);
  1931.       switch (rstat)
  1932.     {
  1933.     case bfd_reloc_ok:
  1934.       break;
  1935.     default:
  1936.     case bfd_reloc_outofrange:
  1937.       abort ();
  1938.     case bfd_reloc_overflow:
  1939.       if (! ((*finfo->info->callbacks->reloc_overflow)
  1940.          (finfo->info,
  1941.           (link_order->type == bfd_section_reloc_link_order
  1942.            ? bfd_section_name (output_bfd,
  1943.                        link_order->u.reloc.p->u.section)
  1944.            : link_order->u.reloc.p->u.name),
  1945.           howto->name, link_order->u.reloc.p->addend,
  1946.           (bfd *) NULL, (asection *) NULL, (bfd_vma) 0)))
  1947.         {
  1948.           free (buf);
  1949.           return false;
  1950.         }
  1951.       break;
  1952.     }
  1953.       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
  1954.                      (file_ptr) link_order->offset, size);
  1955.       free (buf);
  1956.       if (! ok)
  1957.     return false;
  1958.     }
  1959.  
  1960.   /* Store the reloc information in the right place.  It will get
  1961.      swapped and written out at the end of the final_link routine.  */
  1962.  
  1963.   irel = (finfo->section_info[output_section->target_index].relocs
  1964.       + output_section->reloc_count);
  1965.   rel_hash_ptr = (finfo->section_info[output_section->target_index].rel_hashes
  1966.           + output_section->reloc_count);
  1967.  
  1968.   memset (irel, 0, sizeof (struct internal_reloc));
  1969.   *rel_hash_ptr = NULL;
  1970.  
  1971.   irel->r_vaddr = output_section->vma + link_order->offset;
  1972.  
  1973.   if (link_order->type == bfd_section_reloc_link_order)
  1974.     {
  1975.       /* We need to somehow locate a symbol in the right section.  The
  1976.          symbol must either have a value of zero, or we must adjust
  1977.          the addend by the value of the symbol.  FIXME: Write this
  1978.          when we need it.  The old linker couldn't handle this anyhow.  */
  1979.       abort ();
  1980.       *rel_hash_ptr = NULL;
  1981.       irel->r_symndx = 0;
  1982.     }
  1983.   else
  1984.     {
  1985.       struct coff_link_hash_entry *h;
  1986.  
  1987.       h = coff_link_hash_lookup (coff_hash_table (finfo->info),
  1988.                  link_order->u.reloc.p->u.name,
  1989.                  false, false, true);
  1990.       if (h != NULL)
  1991.     {
  1992.       if (h->indx >= 0)
  1993.         irel->r_symndx = h->indx;
  1994.       else
  1995.         {
  1996.           /* Set the index to -2 to force this symbol to get
  1997.          written out.  */
  1998.           h->indx = -2;
  1999.           *rel_hash_ptr = h;
  2000.           irel->r_symndx = 0;
  2001.         }
  2002.     }
  2003.       else
  2004.     {
  2005.       if (! ((*finfo->info->callbacks->unattached_reloc)
  2006.          (finfo->info, link_order->u.reloc.p->u.name, (bfd *) NULL,
  2007.           (asection *) NULL, (bfd_vma) 0)))
  2008.         return false;
  2009.       irel->r_symndx = 0;
  2010.     }
  2011.     }
  2012.  
  2013.   /* FIXME: Is this always right?  */
  2014.   irel->r_type = howto->type;
  2015.  
  2016.   /* r_size is only used on the RS/6000, which needs its own linker
  2017.      routines anyhow.  r_extern is only used for ECOFF.  */
  2018.  
  2019.   /* FIXME: What is the right value for r_offset?  Is zero OK?  */
  2020.  
  2021.   ++output_section->reloc_count;
  2022.  
  2023.   return true;
  2024. }
  2025.  
  2026. /* A basic reloc handling routine which may be used by processors with
  2027.    simple relocs.  */
  2028.  
  2029. boolean
  2030. _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
  2031.                     input_section, contents, relocs, syms,
  2032.                     sections)
  2033.      bfd *output_bfd;
  2034.      struct bfd_link_info *info;
  2035.      bfd *input_bfd;
  2036.      asection *input_section;
  2037.      bfd_byte *contents;
  2038.      struct internal_reloc *relocs;
  2039.      struct internal_syment *syms;
  2040.      asection **sections;
  2041. {
  2042.   struct internal_reloc *rel;
  2043.   struct internal_reloc *relend;
  2044.  
  2045.   rel = relocs;
  2046.   relend = rel + input_section->reloc_count;
  2047.   for (; rel < relend; rel++)
  2048.     {
  2049.       long symndx;
  2050.       struct coff_link_hash_entry *h;
  2051.       struct internal_syment *sym;
  2052.       bfd_vma addend;
  2053.       bfd_vma val;
  2054.       const reloc_howto_type *howto;
  2055.       bfd_reloc_status_type rstat;
  2056.  
  2057.       symndx = rel->r_symndx;
  2058.  
  2059.       if (symndx == -1)
  2060.     {
  2061.       h = NULL;
  2062.       sym = NULL;
  2063.     }
  2064.       else
  2065.     {    
  2066.       h = obj_coff_sym_hashes (input_bfd)[symndx];
  2067.       sym = syms + symndx;
  2068.     }
  2069.  
  2070.       /* COFF treats common symbols in one of two ways.  Either the
  2071.          size of the symbol is included in the section contents, or it
  2072.          is not.  We assume that the size is not included, and force
  2073.          the rtype_to_howto function to adjust the addend as needed.  */
  2074.       if (sym != NULL && sym->n_scnum != 0)
  2075.     addend = - sym->n_value;
  2076.       else
  2077.     addend = 0;
  2078.  
  2079.       howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
  2080.                        sym, &addend);
  2081.       if (howto == NULL)
  2082.     return false;
  2083.  
  2084.       val = 0;
  2085.  
  2086.       if (h == NULL)
  2087.     {
  2088.       asection *sec;
  2089.  
  2090.       if (symndx == -1)
  2091.         {
  2092.           sec = bfd_abs_section_ptr;
  2093.           val = 0;
  2094.         }
  2095.       else
  2096.         {
  2097.           sec = sections[symndx];
  2098.           val = (sec->output_section->vma
  2099.              + sec->output_offset
  2100.              + sym->n_value
  2101.              - sec->vma);
  2102.         }
  2103.     }
  2104.       else
  2105.     {
  2106.       if (h->root.type == bfd_link_hash_defined)
  2107.         {
  2108.           asection *sec;
  2109.  
  2110.           sec = h->root.u.def.section;
  2111.           val = (h->root.u.def.value
  2112.              + sec->output_section->vma
  2113.              + sec->output_offset);
  2114.         }
  2115.       else if (! info->relocateable)
  2116.         {
  2117.           if (! ((*info->callbacks->undefined_symbol)
  2118.              (info, h->root.root.string, input_bfd, input_section,
  2119.               rel->r_vaddr - input_section->vma)))
  2120.         return false;
  2121.         }
  2122.     }
  2123.  
  2124.       rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2125.                     contents,
  2126.                     rel->r_vaddr - input_section->vma,
  2127.                     val, addend);
  2128.  
  2129.       switch (rstat)
  2130.     {
  2131.     default:
  2132.       abort ();
  2133.     case bfd_reloc_ok:
  2134.       break;
  2135.     case bfd_reloc_overflow:
  2136.       {
  2137.         const char *name;
  2138.         char buf[SYMNMLEN + 1];
  2139.  
  2140.         if (symndx == -1)
  2141.           name = "*ABS*";
  2142.         else if (h != NULL)
  2143.           name = h->root.root.string;
  2144.         else if (sym->_n._n_n._n_zeroes == 0
  2145.              && sym->_n._n_n._n_offset != 0)
  2146.           name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
  2147.         else
  2148.           {
  2149.         strncpy (buf, sym->_n._n_name, SYMNMLEN);
  2150.         buf[SYMNMLEN] = '\0';
  2151.         name = buf;
  2152.           }
  2153.  
  2154.         if (! ((*info->callbacks->reloc_overflow)
  2155.            (info, name, howto->name, (bfd_vma) 0, input_bfd,
  2156.             input_section, rel->r_vaddr - input_section->vma)))
  2157.           return false;
  2158.       }
  2159.     }
  2160.     }
  2161.  
  2162.   return true;
  2163. }
  2164.